package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import entity.User;



public class LoginDao {

	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/servlet";
	private static String username = "root";
	private static String password = "root";
	private static Connection conn;

	public static Connection getConnection() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;

	}
	
	
	
	/**
	 * 根据用户名获得用户
	 * @return
	 */
    public User getUserByName(String name){
		try {
			Class.forName(driver);
			Connection connection = DriverManager.getConnection(url, username, password);
			
			String sql = "SELECT * FROM s_user WHERE username = ? ";
			
			PreparedStatement prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, username);
			prepareStatement.setString(2, password);
			ResultSet resultSet = prepareStatement.executeQuery();
			
			while (resultSet.next()) {
				System.out.println(resultSet.getString("username"));
				System.out.println(resultSet.getString("password"));
			}
			
			
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
